import asyncio

from loguru import logger

from com.arcfox.base.base_spider import ExecuteType
from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.school.parse.jzy_major_parse import MajorParser
from com.arcfox.school.processor.jzy.jzy_major_processor import MajorProcessor
from com.arcfox.school.spider.jzy.jzy_base_spider import JZYBaseSpider
from com.arcfox.util import async_request as requests

'''
    聚志愿专业列表抓取
'''


class MajorListSpider(JZYBaseSpider):
    def __init__(self):
        super().__init__()
        self.start_url = f"{self.host}/majors.html"
        self.processor = MajorProcessor()
        self.parser = MajorParser()
        self.task_manager = RedisTaskManager(self.TASK_MAJOR_LIST_JZY)

    async def _pull_task(self):
        return True, ExecuteType.ONCE

    async def _crawl_by_task(self, task):
        await self.init_session()
        resp = await requests.get(self.session, url=self.start_url, headers=self.get_default_header())
        if resp.code == 200:
            major_list = self.parser.parse_major_list(resp.response)
            if len(major_list) == 0:
                logger.info("未查询到专业列表")
                return
            await self.task_manager.add_tasks(major_list)


if __name__ == "__main__":
    asyncio.run(MajorListSpider().start_crawl())
